﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.Configurations;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.ClientSetup;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.Utils;
using System.IO;

namespace TestWebApplication
{
    public class ServiceSettings
    {
        public static string ServerUrl;

        public static string CommerceAccountServiceUrl;

        public static string CommercePaymentInstrumentUrl;

        public static string CommerceTransactionUrl;

        public static string CTPCommerceUrl;

        public static string Connector;

        public static X509Certificate2 cert;

        public static string Password;

        public static string PCSUrl;

        public static string Env;

        public static string WebsiteAddress;

        public static string Api="NONE";

        public static void loadSettings()
        {
            if (Api.Equals("NONE"))
            {
                XmlDocument xml = new XmlDocument();
                String fileLocation = HttpContext.Current.Server.MapPath(Constants.SettingsFileLocation);

                xml.Load(fileLocation);
                XmlElement root = xml.DocumentElement;

                loadServerUrl(root);
                loadConnector(root);
                loadPassword(root);
                loadEnv(root);
                loadWebSiteAddress(root);
                loadApi(root);

                getCert();
                loadPCSUrl(Env);
                getUrls();
                CTPClient.ResetChannelFactory(CTPClientConfiguration.CTPTestCertFile, CTPClientConfiguration.CTPTestCertPassword);
            }
        }

        public static void modifySettings(string serverUrl, string connectorName, string password, string env, string address, string API)
        {
            XmlDocument xml = new XmlDocument();

            xml.Load(HttpContext.Current.Server.MapPath(Constants.SettingsFileLocation));
            XmlElement root = xml.DocumentElement;

            XmlNode serverNode = root.SelectSingleNode("descendant::serverUrl");
            serverNode.InnerText = serverUrl;
            ServerUrl = serverUrl;

            XmlNode certNode = root.SelectSingleNode("descendant::connectorName");
            certNode.InnerText = connectorName;
            Connector = connectorName;

            XmlNode passwordNode = root.SelectSingleNode("descendant::password");
            passwordNode.InnerText = password;
            Password = password;

            XmlNode envNode = root.SelectSingleNode("descendant::env");
            envNode.InnerText = env;
            Env = env;

            XmlNode addressNode = root.SelectSingleNode("descendant::address");
            addressNode.InnerText = address;
            WebsiteAddress = address;

            XmlNode ApiNode = root.SelectSingleNode("descendant::API");
            ApiNode.InnerText = API;
            Api = API;

            getCert();
            loadPCSUrl(Env);
            getUrls();

            CTPClient.ResetChannelFactory(CTPClientConfiguration.CTPTestCertFile, CTPClientConfiguration.CTPTestCertPassword);
        }

        public static void loadWebSiteAddress(XmlElement root)
        {
            XmlNode serverNode = root.SelectSingleNode("descendant::address");
            WebsiteAddress = serverNode.InnerText;
        }

        public static void loadServerUrl(XmlElement root)
        {
            XmlNode serverNode = root.SelectSingleNode("descendant::serverUrl");
            ServerUrl = "https://" + serverNode.InnerText + "/";
            if (CTPClientConfiguration.CTPServer != serverNode.InnerText)
                CTPClientConfiguration.CTPServer = serverNode.InnerText;
        }

        public static void loadConnector(XmlElement root)
        {
            XmlNode node = root.SelectSingleNode("descendant::connectorName");
            Connector = node.InnerText;
        }

        public static void loadPassword(XmlElement root)
        {
            XmlNode node = root.SelectSingleNode("descendant::password");
            Password = node.InnerText;
        }

        public static void loadEnv(XmlElement root)
        {
            XmlNode node = root.SelectSingleNode("descendant::env");
            Env = node.InnerText;
        }

        public static void loadApi(XmlElement root)
        {
            XmlNode node = root.SelectSingleNode("descendant::API");
            Api = node.InnerText;
        }

        public static void getCert()
        {
            string filename = Connector + ".pfx";            
            cert = new X509Certificate2(Path.Combine(CommonUtils.GetAbsolutePath("Cert"), filename), Password);
        }

        public static void loadPCSUrl(string env)
        {
            if (env.Equals("OneBox"))
            {
                PCSUrl = Constants.PCSOneboxUrl;
            }
            else if (env.Equals("Int"))
            {
                PCSUrl = Constants.PCSIntUrl;
            }
            else if (env.Equals("Production"))
            {
                PCSUrl = Constants.PCSProductionUrl;
            }
        }

        public static void getUrls()
        {
            CommerceAccountServiceUrl = ServerUrl + Constants.CommerceAccountSuffix;
            CommercePaymentInstrumentUrl = ServerUrl + Constants.CommercePaymentInstrumentSuffix;
            CommerceTransactionUrl = ServerUrl + Constants.CommerceTransactionSuffix;
        }

    }
}